package com.whl.mybatis.constant;

public class SqlFormat {
	public static final String INSERT_TABLE = "INSERT INTO %s\n";
	public static final String TRIM_PREFIX = "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n";
	public static final String TRIM_SUFFIX = "</trim>\n";
	public static final String IF_TAG_COLUMN = "\t<if test=\"%s != null \">%s,</if>\n";
	public static final String IF_TAG_PROPERTY = "\t<if test=\"%s != null \">#{%s},</if>\n";
	public static final String UPDATE_TABLE = "UPDATE %s\n";
	public static final String SET_PREFIX = "<set>\n";
	public static final String SET_SUFFIX = "</set>\n";
	public static final String IF_TAG_COLUMN_EQ_PROPERTY = "\t<if test=\"%s != null \">%s=#{%s},</if>\n";
	public static final String WHERE_ONE_EQ_ONE = "WHERE 1=1 \n";
	public static final String AND_IF_TAG_COLUMN_EQ_PROPERTY = "\t<if test=\"%s != null \"> and %s=#{%s}</if>\n";
	public static final String SELECT_TABLE = "select * from %s\n";
	public static final String DELETE_TABLE = "delete from %s\n";
	public static final String SCRIPT = "<script>\n\t%s</script>";

	public static String getInsertSql(String tableName) {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(INSERT_TABLE, tableName));
		String column = "column";
		String field = "field";
		builder.append(String.format(TRIM_PREFIX));
		for (int i = 0; i < 3; i++) {
			builder.append(String.format(IF_TAG_PROPERTY, field + i, column + i));
		}
		builder.append(String.format(TRIM_SUFFIX));

		builder.append(String.format(TRIM_PREFIX));
		for (int i = 0; i < 3; i++) {
			builder.append(String.format(IF_TAG_COLUMN, field + i, column + i));
		}
		builder.append(String.format(TRIM_SUFFIX));

		System.out.println(builder);
		return builder.toString();
	}

	public static String getUpdateSql(String tableName) {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(UPDATE_TABLE, tableName));
		String column = "column";
		String field = "field";
		builder.append(String.format(SET_PREFIX));
		for (int i = 0; i < 3; i++) {
			builder.append(String.format(IF_TAG_COLUMN_EQ_PROPERTY, field + i, column + i, field + i));
		}
		builder.append(String.format(SET_SUFFIX));
		builder.append(String.format(WHERE_ONE_EQ_ONE));
		builder.append(String.format(AND_IF_TAG_COLUMN_EQ_PROPERTY, "field0", "column0", "field0"));

		System.out.println(builder);
		return builder.toString();
	}

	public static String getSelectSql(String tableName) {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(SELECT_TABLE, tableName));
		builder.append(String.format(WHERE_ONE_EQ_ONE));
		builder.append(String.format(AND_IF_TAG_COLUMN_EQ_PROPERTY, "field0", "column0", "field0"));

		System.out.println(builder);
		return builder.toString();
	}

	public static String getDeleteSql(String tableName) {
		StringBuilder builder = new StringBuilder();
		builder.append(String.format(DELETE_TABLE, tableName));
		builder.append(String.format(WHERE_ONE_EQ_ONE));
		builder.append(String.format(AND_IF_TAG_COLUMN_EQ_PROPERTY, "field0", "column0", "field0"));

		System.out.println(builder);
		return builder.toString();
	}

}
